/*
 * Created by JFormDesigner on Sat Jun 10 08:57:53 CST 2023
 */

package fm.trade;

import java.awt.event.*;

import dao.Dao_trade;

import java.awt.*;
import java.util.List;
import java.util.Vector;
import javax.swing.*;

/**
 * @author YangC
 */
public class TradeAdd extends JInternalFrame {
    public TradeAdd() {
        initComponents();
        initData();
    }

    public void initData(){
        Vector<String> its = Dao_trade.getTradeType();
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
        typeComboBox.setModel(model);
    }

    private void Cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void Add(ActionEvent e) {
        List<String> list = Dao_trade.getTradeID();

        if(tradeIDTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入商品编号");
            return;
        }

        for (String s : list) {
            if (s.equals(tradeIDTextField.getText())) {
                JOptionPane.showMessageDialog(null, "商品ID已存在，请重新设置商品编号！");
                return;
            }
        }

        if(tradeIDTextField.getText().length() != 5)
        {
            JOptionPane.showMessageDialog(null,"商品编号应为5位");
            return;
        }

        if(tradeNameTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入商品名称");
            return;
        }

        if(priceTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入单价");
            return;
        }

        if(quantityTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入商品数量");
            return;
        }

        if(discountField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入折扣");
            return;
        }

        Double rap = Double.valueOf(discountField.getText().trim());
        if(rap > 1 || rap ==0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的折扣！");
            return;
        }

        Double yc = Double.valueOf(quantityTextField.getText().trim());
        if (yc <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的数量！");
            return;
        }

        Double ycy = Double.valueOf(priceTextField.getText().trim());
        if(ycy <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的单价！");
            return;
        }

        String tradeID = tradeIDTextField.getText().trim();

        String selectedItem = (String)typeComboBox.getSelectedItem();
        String tradeType = Dao_trade.getTypeID_Name(selectedItem);

        String tradeName = tradeNameTextField.getText();
        Double price = Double.valueOf(priceTextField.getText());
        String quantity = quantityTextField.getText();
        Double discount = Double.valueOf(discountField.getText());

        int i = Dao_trade.insert(tradeID,tradeType,tradeName,quantity,price,discount);

        if (i == 1){
            JOptionPane.showMessageDialog(null,"添加成功！");
            doDefaultCloseAction();
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        AddButton = new JButton();
        CancelButton = new JButton();
        panel2 = new JPanel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        typeComboBox = new JComboBox();
        tradeIDTextField = new JTextField();
        tradeNameTextField = new JTextField();
        priceTextField = new JTextField();
        quantityTextField = new JTextField();
        label8 = new JLabel();
        quantityTextField2 = new JTextField();
        label9 = new JLabel();
        label10 = new JLabel();
        quantityTextField3 = new JTextField();
        label11 = new JLabel();
        quantityTextField4 = new JTextField();
        label12 = new JLabel();
        discountField = new JTextField();
        label1 = new JLabel();

        //======== this ========
        setVisible(true);
        setTitle("\u6dfb\u52a0\u5546\u54c1");
        setMinimumSize(new Dimension(89, 180));
        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(328, 60));
            panel1.setLayout(null);

            //---- AddButton ----
            AddButton.setText("\u6dfb\u52a0");
            AddButton.addActionListener(e -> Add(e));
            panel1.add(AddButton);
            AddButton.setBounds(150, 15, AddButton.getPreferredSize().width, 26);

            //---- CancelButton ----
            CancelButton.setText("\u53d6\u6d88");
            CancelButton.addActionListener(e -> Cancel(e));
            panel1.add(CancelButton);
            CancelButton.setBounds(280, 13, CancelButton.getPreferredSize().width, 27);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 269, 478, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setMinimumSize(new Dimension(455, 140));
            panel2.setPreferredSize(new Dimension(455, 140));
            panel2.setMaximumSize(new Dimension(32767, 160));
            panel2.setLayout(null);

            //---- label2 ----
            label2.setText("\u5546\u54c1\u7f16\u53f7\uff1a");
            panel2.add(label2);
            label2.setBounds(33, 26, label2.getPreferredSize().width, 22);

            //---- label3 ----
            label3.setText("\u5546\u54c1\u7c7b\u578b\uff1a");
            panel2.add(label3);
            label3.setBounds(241, 28, 60, 22);

            //---- label4 ----
            label4.setText("\u5546\u54c1\u540d\u79f0\uff1a");
            panel2.add(label4);
            label4.setBounds(33, 81, 60, 22);

            //---- label5 ----
            label5.setText("\u6570\u91cf");
            panel2.add(label5);
            label5.setBounds(241, 83, 60, 22);

            //---- label6 ----
            label6.setText("\u5355\u4ef7\uff1a");
            panel2.add(label6);
            label6.setBounds(33, 136, 60, 22);
            panel2.add(typeComboBox);
            typeComboBox.setBounds(306, 24, 139, typeComboBox.getPreferredSize().height);
            panel2.add(tradeIDTextField);
            tradeIDTextField.setBounds(95, 22, 135, 28);
            panel2.add(tradeNameTextField);
            tradeNameTextField.setBounds(95, 78, 134, 28);
            panel2.add(priceTextField);
            priceTextField.setBounds(94, 134, 135, 28);
            panel2.add(quantityTextField);
            quantityTextField.setBounds(306, 79, 139, 28);

            //---- label8 ----
            label8.setText("\u6570\u91cf\uff1a");
            panel2.add(label8);
            label8.setBounds(33, 328, 60, 22);
            panel2.add(quantityTextField2);
            quantityTextField2.setBounds(95, 325, 134, 28);

            //---- label9 ----
            label9.setPreferredSize(new Dimension(0, 80));
            panel2.add(label9);
            label9.setBounds(0, 447, 458, 80);

            //---- label10 ----
            label10.setText("\u6570\u91cf\uff1a");
            panel2.add(label10);
            label10.setBounds(33, 608, 60, 22);
            panel2.add(quantityTextField3);
            quantityTextField3.setBounds(95, 605, 134, 28);

            //---- label11 ----
            label11.setText("\u6570\u91cf\uff1a");
            panel2.add(label11);
            label11.setBounds(33, 361, 60, 22);
            panel2.add(quantityTextField4);
            quantityTextField4.setBounds(95, 358, 134, 28);

            //---- label12 ----
            label12.setText("\u6298\u6263\uff1a");
            panel2.add(label12);
            label12.setBounds(243, 135, label12.getPreferredSize().width, 24);
            panel2.add(discountField);
            discountField.setBounds(306, 132, 139, 28);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 105, 478, 174);

        //---- label1 ----
        label1.setIcon(new ImageIcon(getClass().getResource("/ref/img_1.png")));
        contentPane.add(label1);
        label1.setBounds(-5, -90, 550, 260);

        contentPane.setPreferredSize(new Dimension(490, 360));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton AddButton;
    private JButton CancelButton;
    private JPanel panel2;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JComboBox typeComboBox;
    private JTextField tradeIDTextField;
    private JTextField tradeNameTextField;
    private JTextField priceTextField;
    private JTextField quantityTextField;
    private JLabel label8;
    private JTextField quantityTextField2;
    private JLabel label9;
    private JLabel label10;
    private JTextField quantityTextField3;
    private JLabel label11;
    private JTextField quantityTextField4;
    private JLabel label12;
    private JTextField discountField;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
